import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import AppLayout from '@/components/layout/AppLayout.vue'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    component: AppLayout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '首页' }
      },
      // 测试管理路由
      {
        path: 'test',
        children: [
          {
            path: 'platform',
            name: 'TestPlatform',
            component: () => import('@/views/test/TestPlatform.vue'),
            meta: { title: '测试平台' }
          },
          {
            path: 'cases',
            name: 'TestCases',
            component: () => import('@/views/test/TestCases.vue'),
            meta: { title: '测试用例列表' }
          },
          {
            path: 'create-case',
            name: 'CreateTestCase',
            component: () => import('@/views/test/CreateTestCase.vue'),
            meta: { title: '创建/更新测试用例' }
          },
          {
            path: 'bind-cases',
            name: 'BindTestCases',
            component: () => import('@/views/test/BindTestCases.vue'),
            meta: { title: '绑定测试用例' }
          },
          {
            path: 'script',
            name: 'TestScript',
            component: () => import('@/views/test/TestScript.vue'),
            meta: { title: '编辑/查看执行脚本' }
          }
        ]
      },
      // 任务管理路由
      {
        path: 'task',
        children: [
          {
            path: 'list',
            name: 'TaskList',
            component: () => import('@/views/task/TaskList.vue'),
            meta: { title: '任务管理' }
          },
          {
            path: 'create',
            name: 'CreateTask',
            component: () => import('@/views/task/CreateTask.vue'),
            meta: { title: '创建任务' }
          },
          {
            path: 'details/:id?',
            name: 'TaskDetails',
            component: () => import('@/views/task/TaskDetails.vue'),
            meta: { title: '任务详情' }
          },
          {
            path: 'execute',
            name: 'ExecuteTask',
            component: () => import('@/views/task/ExecuteTask.vue'),
            meta: { title: '执行任务' }
          },
          {
            path: 'results',
            name: 'TaskResults',
            component: () => import('@/views/task/TaskResults.vue'),
            meta: { title: '任务执行结果' }
          },
          {
            path: 'result-details/:id?',
            name: 'TaskResultDetails',
            component: () => import('@/views/task/TaskResultDetails.vue'),
            meta: { title: '执行结果详情' }
          }
        ]
      },
      // 设备管理路由
      {
        path: 'device',
        children: [
          {
            path: 'list',
            name: 'DeviceList',
            component: () => import('@/views/device/DeviceList.vue'),
            meta: { title: '设备管理' }
          },
          {
            path: 'create',
            name: 'CreateDevice',
            component: () => import('@/views/device/CreateDevice.vue'),
            meta: { title: '创建设备' }
          },
          {
            path: 'details/:id?',
            name: 'DeviceDetails',
            component: () => import('@/views/device/DeviceDetails.vue'),
            meta: { title: '设备详情' }
          },
          {
            path: 'test',
            name: 'DeviceTest',
            component: () => import('@/views/device/DeviceTest.vue'),
            meta: { title: '设备测试' }
          },
          {
            path: 'process',
            name: 'DeviceProcess',
            component: () => import('@/views/device/DeviceProcess.vue'),
            meta: { title: '设备进程管理' }
          }
        ]
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/dashboard'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 测试管理平台`
  }
  
  next()
})

export default router
